package producersConsumersTests.WaitingTime;

import prod_cons.TaskContainer;
import producersConsumersTests.WaitingTime.TimedTask.WhenToMeasure;
import producersConsumersTests.utils.Inserter;
import producersConsumersTests.utils.TaskFactory;

/**
 * TimeSettingInserter is an inserter that attaches the insertion time to the
 * task. The actual attachment is performed by the TimedTaskFactory.
 * 
 */
public class TimeSettingInserter extends Inserter<TimedTask> {
	private final WhenToMeasure when;
	public static class SmallTimedTaskFactory implements TaskFactory<TimedTask> {
		@Override
		public TimedTask createTask(long taskId) {
			TimedTask t = new TimedTask(taskId);
			return t;
		}
	}

	public TimeSettingInserter(TaskContainer container, int peakLength,
			int timeBetweenPeaks, WhenToMeasure when) {
		super(container, peakLength, timeBetweenPeaks,
				new SmallTimedTaskFactory());
		this.when = when;
	}

	public TimeSettingInserter(TaskContainer container, double lambda,
			WhenToMeasure when) {
		super(container, lambda, new SmallTimedTaskFactory());
		this.when = when;
	}

	@Override
	protected void beforeInsert(TimedTask task) {
		if (when == WhenToMeasure.BEFORE)
			task.setEnterTime(System.nanoTime());
	}

	@Override
	protected void afterInsert(TimedTask task) {
		if (when == WhenToMeasure.AFTER)
			task.setEnterTime(System.nanoTime());
	}
}
